import * as echarts from '../../ec-canvas/echarts';

const app = getApp();

Page({
  data: {
    TabCur: 0,
    type:'周',
    scrollLeft:0,
    week_data:[],
    loading:false,
    show_week:true,
    show_month:false,
    ec: {
      onInit: initChart
    },
    data_type:['周','月','年']
  },

  onReady() {

  },

  onShow:function(){
    let _this = this;
    _this.week_init();
    setTimeout(function() {
      _this.setData({
        loading: true
      })
    }, 500)
    wx.showModal({
      title: '官方提示',
      content: '数据调整中',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //初始化
  week_init:function(){
    let _this = this,
      week_data = _this.data.week_data;
    app._post('api/base/week_init', {}, function(result) {
      for(var i=result.data.start;i<=result.data.end;i++){
        week_data.push(i);
      }
      _this.setData({
        week_data:week_data,
        TabCur:result.data.end
      })
    });
  },

  //点击选择周
  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id-1)*60
    })
  },

  //点击选择周
  typeSelect(e) {
    let _this = this,
     show_week = _this.data.show_week,
     show_year = _this.data.show_year,
     type = e.currentTarget.dataset.id;

    if(type == '月' || type == '年'){
      show_week = false;
    }
    _this.setData({
      type: type,
      show_week: show_week
    })
  }
});



//数据渲染
function initChart(canvas, width, height) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height
  });
  canvas.setChart(chart);

  var option = {
    legend: {
      data: ['收入','支出'],
      left: 'center',
      backgroundColor: 'white',
      z: 100
    },
    grid: {
      containLabel: true,
      left:'2%',
      right:'5%',
      bottom:'2%',
      top:'15%'
    },
    tooltip: {
      show: true,
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'],
    },
    yAxis: {
      x: 'center',
      type: 'value',
      splitLine: {
        lineStyle: {
          type: 'dashed'
        }
      }
    },
    series: [{
      name: '收入',
      type: 'line',
      smooth: true,
      data: [18, 36, 65, 30, 78, 40, 150],
      itemStyle : {  
        normal : {  
            color:'#23aa19',
            lineStyle:{  
                color:'#23aa19'  
            }  
        }  
      }, 
    },
    {
      name: '支出',
      type: 'line',
      smooth: true,
      data: [33, 40, 78, 20, 65, 36, 18],
      itemStyle : {  
        normal : {  
            color:'#1d5dbd',
            lineStyle:{  
                color:'#1d5dbd'  
            }  
        }  
      }, 
    }
    ]
  };

  chart.setOption(option);
  return chart;
}


